﻿using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Win32;

namespace uTILLIty.Outlook.FlexFileAddIn
{
	[RunInstaller(true)]
	public partial class FlexFileInstaller : System.Configuration.Install.Installer
	{
		const string RegRoot = @"Software\Microsoft\Office\Outlook\Addins";
		const string RegFolder = "uTILLIty.Outlook.FlexFileAddIn";

		public FlexFileInstaller()
		{
			InitializeComponent();
		}

		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
		public override void Install(IDictionary stateSaver)
		{
			base.Install(stateSaver);

			//CustomActionData: /allusers=[ALLUSERS] /targetdir=[TARGETDIR]
			//bool installToAllUsers = Context.Parameters["allusers"] == "1";
			//var path = Context.Parameters["targetdir"];
			//Debugger.Launch();
			var root = Registry.LocalMachine;
			var regPath = $"{RegRoot}\\{RegFolder}";
			var key = root.OpenSubKey(regPath, true);
			if (key != null)
			{
				var manifest = (string) key.GetValue("Manifest", "");
				manifest = manifest.Replace('\\', '/');
				key.SetValue("Manifest", manifest);
			}
		}

		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
		public override void Commit(IDictionary savedState)
		{
			base.Commit(savedState);
			Process.Start("http://outlookflexfile.codeplex.com/");
		}

		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
		public override void Rollback(IDictionary savedState)
		{
			base.Rollback(savedState);
		}

		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
		public override void Uninstall(IDictionary savedState)
		{
			base.Uninstall(savedState);
		}
	}
}